package com.kmxd.ams.app.watermark;

import com.kmxd.ams.client.archive.IArchiveWatermarkSvc;
import com.kmxd.ams.client.archive.vo.ArchiveWatermarkVO;
import com.kmxd.ams.client.watermark.IWatermarkSvc;
import com.kmxd.ams.client.watermark.vo.WatermarkVO;
import com.kmxd.ams.core.common.constant.Constants;
import com.kmxd.ams.core.common.enums.ArchWatermarkTypeEnum;
import com.kmxd.ams.core.oss.template.OssTemplate;
import com.kmxd.ams.core.util.WatermarkUtil;
import java.io.File;
import java.util.Objects;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * description
 *
 * @author nickbi
 */
@Service(Constants.WATERMARK_SVC_PREFIX + "pdfjs")
@AllArgsConstructor
public class WatermarkPdfSvcImpl implements IWatermarkSvc {

  private final IArchiveWatermarkSvc archiveWatermarkSvc;
  private final OssTemplate ossTemplate;

  @Override
  public WatermarkVO addWatermark(Long archTypeId, File file, String waterMarkFilePath) {
    if (Objects.isNull(file)) {
      return WatermarkVO.builder().build();
    }
    ArchiveWatermarkVO watermark = archiveWatermarkSvc.getByArchTypeId(archTypeId);
    if (Objects.isNull(watermark)) {
      return WatermarkVO.builder().build();
    }

    // 目录不存在，创建目录
    if (!ossTemplate.exist(waterMarkFilePath)) {
      File tmpFile = new File(waterMarkFilePath);
    }
    if (watermark.getType().equals(ArchWatermarkTypeEnum.IMAGE.getType())) {
      String imagePath = ossTemplate.getAttachmentPath(null) + watermark.getPicPath();
      WatermarkUtil.addPdfWatermark(
          file.getPath(),
          waterMarkFilePath,
          null,
          watermark.getOpacity(),
          watermark.getAngle(),
          watermark.getFontSize(),
          imagePath,
          0,
          0,
          watermark.getAngle());
    } else {
      String text =
          watermark.getType().equals(ArchWatermarkTypeEnum.TEXT.getType())
              ? watermark.getText()
              : WatermarkUtil.format(watermark.getDynamicInfo());
      WatermarkUtil.addPdfWatermark(
          file.getPath(),
          waterMarkFilePath,
          text,
          watermark.getOpacity(),
          watermark.getAngle(),
          watermark.getFontSize());
    }

    return WatermarkVO.builder()
        .file(ossTemplate.getFile(waterMarkFilePath))
        .path(waterMarkFilePath)
        .build();
  }
}
